// pages/shopManager/goodsManager/batchManager/batchManager.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsType: null,
        goodsStatus: null,
        classifyId: null,
        selectGoods: [],
        goodsList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false,
        isWorking: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.classifyId) {
            this.setData({
                goodsType: options.goodsType,
                goodsStatus: options.type,
                classifyId: options.classifyId
            })
        } else {
            this.setData({
                goodsType: options.goodsType,
                goodsStatus: options.type
            })
        }
        this.getGoodsList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
        let pages = getCurrentPages(); //获取当前页面实例
        let prepage = pages[pages.length - 2]
        if (prepage == undefined || prepage == null) return;
        prepage.updateData()
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setData({
            goodsList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getGoodsList()
    },

    checkboxChange: function(e) {
        this.setData({
            selectGoods: e.detail.value
        })
    },

    getGoodsList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let goodsList = this.data.goodsList
        let data
        if (this.data.goodsType == 'normal') {
            if (this.data.goodsStatus == 'inSale') {
                if (this.data.classifyId == null) {
                    data = {
                        store_id: wx.getStorageSync('storeId'),
                        is_sale: this.data.goodsStatus,
                        is_goods: '0',
                        is_sale: '1',
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else {
                    data = {
                        class_id: this.data.classifyId,
                        store_id: wx.getStorageSync('storeId'),
                        is_sale: this.data.goodsStatus,
                        is_goods: '0',
                        is_sale: '1',
                        limit: 10,
                        page: this.data.nowPage
                    }
                }
            } else {
                if (this.data.classifyId == null) {
                    data = {
                        store_id: wx.getStorageSync('storeId'),
                        is_sale: this.data.goodsStatus,
                        is_goods: '0',
                        is_sale: '0',
                        limit: 10,
                        page: this.data.nowPage
                    }
                } else {
                    data = {
                        class_id: this.data.classifyId,
                        store_id: wx.getStorageSync('storeId'),
                        is_sale: this.data.goodsStatus,
                        is_goods: '0',
                        is_sale: '0',
                        limit: 10,
                        page: this.data.nowPage
                    }
                }
            }
        } else if (this.data.goodsType == 'vip') {
            if (this.data.goodsStatus == 'inSale') {
                data = {
                    store_id: wx.getStorageSync('storeId'),
                    is_sale: this.data.goodsStatus,
                    is_goods: '1',
                    is_sale: '1',
                    limit: 10,
                    page: this.data.nowPage
                }
            } else {
                data = {
                    store_id: wx.getStorageSync('storeId'),
                    is_sale: this.data.goodsStatus,
                    is_goods: '1',
                    is_sale: '0',
                    limit: 10,
                    page: this.data.nowPage,
                    client: 2
                }
            }
        } else if (this.data.goodsType == 'agent') {
            if (this.data.goodsStatus == 'inSale') {
                data = {
                    store_id: wx.getStorageSync('storeId'),
                    is_sale: this.data.goodsStatus,
                    is_goods: '1',
                    agency_goods: '1',
                    is_sale: '1',
                    limit: 10,
                    page: this.data.nowPage
                }
            } else {
                data = {
                    store_id: wx.getStorageSync('storeId'),
                    is_sale: this.data.goodsStatus,
                    is_goods: '1',
                    agency_goods: '1',
                    is_sale: '0',
                    limit: 10,
                    page: this.data.nowPage,
                    client: 2
                }
            }
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Goods/goods_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    goodsList: goodsList,
                    isAll: true
                })
            } else {
                goodsList = goodsList.concat(res)
                that.setData({
                    goodsList: goodsList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },
    sellGoods: function() {
        if (this.data.isWorking) {
            return
        }
        if (this.data.selectGoods.length == 0) {
            wx.showToast({
                title: '请选择商品',
                icon: 'none',
                duration: 2000
            })
            return
        }
        this.setData({
            isWorking: true
        })
        wx.showLoading({
            title: '请稍后'
        })
        const that = this
        let data = {
            goods_id: this.data.selectGoods,
            is_sale: '1'
        }
        common.post('Goods/del_goods', data).then(function(res) {
            that.setData({
                isWorking: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
                return
            }
            wx.showModal({
                title: '提示',
                content: '上架商品成功',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            selectGoods: [],
                            goodsList: [],
                            nowPage: 0,
                            isLoading: false,
                            isAll: false
                        })
                        that.getGoodsList()
                    }
                }
            })
        }).catch(function(e) {})
    },

    downGoods: function() {
        if (this.data.isWorking) {
            return
        }
        if (this.data.selectGoods.length == 0) {
            wx.showToast({
                title: '请选择商品',
                icon: 'none',
                duration: 2000
            })
            return
        }
        this.setData({
            isWorking: true
        })
        wx.showLoading({
            title: '请稍后'
        })
        const that = this
        let data = {
            goods_id: this.data.selectGoods,
            is_sale: '0'
        }
        common.post('Goods/del_goods', data).then(function(res) {
            that.setData({
                isWorking: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
                return
            }
            wx.showModal({
                title: '提示',
                content: '下架商品成功',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            selectGoods: [],
                            goodsList: [],
                            nowPage: 0,
                            isLoading: false,
                            isAll: false
                        })
                        that.getGoodsList()
                    }
                }
            })
        }).catch(function(e) {})
    },

    deleteGoods: function() {
        if (this.data.isWorking) {
            return
        }
        if (this.data.selectGoods.length == 0) {
            wx.showToast({
                title: '请选择商品',
                icon: 'none',
                duration: 2000
            })
            return
        }
        this.setData({
            isWorking: true
        })
        wx.showLoading({
            title: '请稍后'
        })
        const that = this
        let data = {
            goods_id: this.data.selectGoods
        }
        common.post('Goods/delate_goods', data).then(function(res) {
            that.setData({
                isWorking: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
                return
            }
            wx.showModal({
                title: '提示',
                content: '删除商品成功',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            selectGoods: [],
                            goodsList: [],
                            nowPage: 0,
                            isLoading: false,
                            isAll: false
                        })
                        that.getGoodsList()
                    }
                }
            })
        }).catch(function(e) {})
    }
})